package com.google.api.adwords.starterkit.Synchronizer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;

import com.google.api.adwords.starterkit.settings.StarterKitSettings;
import com.google.api.adwords.starterkit.utils.DumpExceptions;
import com.google.api.adwords.v201101.cm.CampaignTargetOperation;
import com.google.api.adwords.v201101.cm.Operator;
import com.google.api.adwords.v201101.cm.PlatformTarget;
import com.google.api.adwords.v201101.cm.PlatformTargetList;
import com.google.api.adwords.v201101.cm.PlatformType;

/**
 * The Class PlatformTargetSynchronizer is responsible to keep all 
 * PlatformTarget information from AdWords system synchronized with local DB.
 * 
 * @author rodrigo.f.vale@gmail.com (Rodrigo Vale)
 */
public class PlatformTargetSynchronizer extends TargetsSynchronizer {

  public PlatformTargetSynchronizer(StarterKitSettings starterKitSetting, 
      Connection conn) {
    super(starterKitSetting, conn);  
  }

  /**
   * Process the updates in platformTarget table.
   */
  public void put() {
    log.debug("Preparing Platform to be sent to AdWords");
    platformTargetTable.updateCampaignsIds();
  
    ResultSet rs = platformTargetTable.getModified();
    try {      
      HashSet<CampaignTargetOperation> campaignTargetOperation 
          = new HashSet<CampaignTargetOperation>();
      HashSet<PlatformTarget> platformTarget = new HashSet<PlatformTarget>();
      long lastCampaignId = 0;
      long campaignId = 0;
      PlatformType platform;
      while (rs.next()) {
        campaignId = rs.getLong("campaignId");
        platform = PlatformType.fromString(rs.getString("platform"));
        if (lastCampaignId != campaignId) {
          if (platformTarget.size() > 0) {
            PlatformTargetList platformTargetList = new PlatformTargetList();
            platformTargetList.setCampaignId(lastCampaignId);
            platformTargetList.setTargets(platformTarget
                .toArray(new PlatformTarget[platformTarget.size()]));
            CampaignTargetOperation platformTargetOperation 
                = new CampaignTargetOperation();
            platformTargetOperation.setOperand(platformTargetList);
            platformTargetOperation.setOperator(Operator.SET);
            campaignTargetOperation.add(platformTargetOperation);
          }
          lastCampaignId = campaignId;
          platformTarget.clear();
        }
        platformTarget.add(new PlatformTarget(null, platform));
      }
      rs.close();
      campaignTable.resetPlatformTargetFlags();
  
      if (platformTarget.size() > 0) {
        PlatformTargetList platformTargetList = new PlatformTargetList();
        platformTargetList.setCampaignId(lastCampaignId);
        platformTargetList.setTargets(platformTarget
            .toArray(new PlatformTarget[platformTarget.size()]));
  
        CampaignTargetOperation platformTargetOperation 
            = new CampaignTargetOperation();
        platformTargetOperation.setOperand(platformTargetList);
        platformTargetOperation.setOperator(Operator.SET);
        campaignTargetOperation.add(platformTargetOperation);
      }
      processOperations(campaignTargetOperation);
    } catch (SQLException e) {
      DumpExceptions.dumpSQLException(e);
    }
    campaignTable.resetPlatformTargetFlags();
  }

}
